package com.mine.yyb.modules.sys.dao.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.jdbc.MineBaseDao;
import com.mine.yyb.modules.sys.dao.INoticeDao;
import com.mine.yyb.modules.sys.vo.NoticeVO;

@Repository
public class NoticeDaoImpl extends MineBaseDao implements INoticeDao
{
    private static Logger logger = LoggerFactory.getLogger(NoticeDaoImpl.class);
    
    @Override
    public int save(NoticeVO noticeVO)
        throws MineException
    {
        try
        {
            logger.info(noticeVO.toString());
            
            StringBuffer sql = new StringBuffer();
            
            sql.append(" insert into " + NoticeVO.TABLE_NAME);
            sql.append(" (content,status,createDate,updateDate)");
            sql.append(" values(:content,:status,now(),now())");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), noticeVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int update(NoticeVO noticeVO)
        throws MineException
    {
        try
        {
            logger.info(noticeVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append(" update " + NoticeVO.TABLE_NAME);
            sql.append(" set updateDate = now()");
            if (StringUtils.isNotBlank(noticeVO.getContent()))
            {
                sql.append(" ,content = :content");
            }
            if (noticeVO.getStatus() != 0)
            {
                sql.append(" ,status=:status");
            }
            sql.append(" where 1 order by updateDate desc limit 1 ");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), noticeVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int delete(NoticeVO noticeVO)
        throws MineException
    {
        try
        {
            logger.info(noticeVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append("delete from " + NoticeVO.TABLE_NAME);
            sql.append(" where id = :id");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), noticeVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public NoticeVO queryNotice(NoticeVO noticeVO)
        throws MineException
    {
        try
        {
            logger.info(noticeVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append("select id, content, status from " + NoticeVO.TABLE_NAME + " ");
            sql.append("where 1 = 1 ");
            if(noticeVO.getStatus() != 0)
            {
                sql.append("and status = :status ");
            }
            sql.append("order by updateDate desc limit 1");
            
            logger.info(sql.toString());
            return super.queryForObject(sql.toString(), noticeVO, NoticeVO.class);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
}
